package com.fept.msgcore.msgserver.util.system;

/**
 * Created by Srma on 1/12/2017.
 */
public class ByteUtil {
    public ByteUtil() {
    }

    public static int getIntFromBytes(byte[] data, int offset, int length) {
        int tmp = 0;

        for(int i = offset; i < offset + length && i < data.length; ++i) {
            tmp = tmp << 8 & -256 | data[i] & 255;
        }

        return tmp;
    }

    public static long getLongFromBytes(byte[] data, int offset, int length) {
        long tmp = 0L;

        for(int i = offset; i < offset + length && i < data.length; ++i) {
            tmp = tmp << 8 & -256L | (long)(data[i] & 255);
        }

        return tmp;
    }

    public static String fromAsciiBytes(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        byte[] var5 = buf;
        int var4 = buf.length;

        for(int var3 = 0; var3 < var4; ++var3) {
            byte b = var5[var3];
            sb.append((char)b);
        }

        return sb.toString();
    }

    public static boolean isValidAscii(byte b) {
        return (b & -128) == 0;
    }

    public static boolean isValidAscii(byte[] buf) {
        byte[] var4 = buf;
        int var3 = buf.length;

        for(int var2 = 0; var2 < var3; ++var2) {
            byte b = var4[var2];
            if(!isValidAscii(b)) {
                return false;
            }
        }

        return true;
    }

    public static boolean isValidAsciiNumber(byte b) {
        return b >= 48 && b <= 57;
    }

    public static boolean isValidAsciiNumber(byte[] buf) {
        byte[] var4 = buf;
        int var3 = buf.length;

        for(int var2 = 0; var2 < var3; ++var2) {
            byte b = var4[var2];
            if(!isValidAsciiNumber(b)) {
                return false;
            }
        }

        return true;
    }

    public static boolean isValidAsciiNumberAndAlphabet(byte b) {
        return b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122;
    }

    public static boolean isValidAsciiNumberAndAlphabet(byte[] buf) {
        byte[] var4 = buf;
        int var3 = buf.length;

        for(int var2 = 0; var2 < var3; ++var2) {
            byte b = var4[var2];
            if(!isValidAsciiNumberAndAlphabet(b)) {
                return false;
            }
        }

        return true;
    }

    public static void bytesAssign(byte[] dst, byte[] src) {
        if(dst != null && src != null) {
            System.arraycopy(src, 0, dst, 0, dst.length < src.length?dst.length:src.length);
        }
    }

    public static char byteToChar(byte b) {
        return (char)b;
    }

    public static String dumpMessage(byte[] hdr, byte[] buf, int nbytes) {
        StringBuilder sb = new StringBuilder();
        int c;
        if(nbytes <= 0) {
            if(hdr != null && hdr.length > 0) {
                for(c = 0; c < hdr.length; ++c) {
                    sb.append(String.format("%02X ", new Object[]{Byte.valueOf(hdr[c])}));
                }
            }

            if(buf != null && buf.length > 0) {
                for(c = 0; c < buf.length; ++c) {
                    sb.append(String.format("%02X ", new Object[]{Byte.valueOf(buf[c])}));
                }
            }
        } else {
            c = 0;
            int i;
            if(hdr != null && hdr.length > 0) {
                for(i = 0; i < hdr.length; ++c) {
                    if(c >= nbytes && c % nbytes == 0) {
                        sb.append("\n");
                    }

                    sb.append(String.format("%02X ", new Object[]{Byte.valueOf(hdr[i])}));
                    ++i;
                }
            }

            if(buf != null && buf.length > 0) {
                for(i = 0; i < buf.length; ++c) {
                    if(c >= nbytes && c % nbytes == 0) {
                        sb.append("\n");
                    }

                    sb.append(String.format("%02X ", new Object[]{Byte.valueOf(buf[i])}));
                    ++i;
                }
            }
        }

        return sb.toString();
    }

    public static String dumpMessage(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        if(bytes != null && bytes.length > 0) {
            for(int i = 0; i < bytes.length; ++i) {
                sb.append(String.format("%02X ", new Object[]{Byte.valueOf(bytes[i])}));
            }
        }

        return sb.toString();
    }

    public static String dumpMessageWithAlignment(byte[] bytes, int nbytes) {
        StringBuilder sb = new StringBuilder();
        if(bytes != null && bytes.length > 0) {
            for(int i = 0; i < bytes.length; ++i) {
                if(i >= nbytes && i % nbytes == 0) {
                    sb.append("\n");
                }

                sb.append(String.format("%02X ", new Object[]{Byte.valueOf(bytes[i])}));
            }
        }

        return sb.toString();
    }

    public static String bytesToHexString(byte[] bytes) {
        return bytesToHexString(bytes, (String)null);
    }

    public static String bytesToHexString(byte[] bytes, String delimiter) {
        if(bytes == null) {
            return null;
        } else if(bytes.length < 1) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("%02X", new Object[]{Byte.valueOf(bytes[0])}));
            if(delimiter == null) {
                delimiter = "";
            }

            for(int i = 1; i < bytes.length; ++i) {
                sb.append(delimiter).append(String.format("%02X", new Object[]{Byte.valueOf(bytes[i])}));
            }

            return sb.toString();
        }
    }
}

